import { Component, ViewChild, OnInit, EventEmitter, Input, Output, Host, SimpleChanges } from '@angular/core';
import { UtilsService } from '../../../../shared/services/utils.service';
import { ResearchEsgRatingService } from '../researchEsgRating.service';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { AppComponent } from '../../../../app.component';
import { Subscription } from 'rxjs/Subscription';
import { CompanyCountModel } from '../../../../shared/models/companyCount.model';
import { EsgFilterModel } from '../models/esgFilter.model';
import { EsgListModel } from '../models/esgList.model';
import { ColumnOptions } from '../../../../shared/components/table/models/columnOptions.model';
import { TableComponent, PaginationPositionDisplayEnum } from '../../../../shared/components/table/table.component';
import { TableOptions } from '../../../../shared/components/table/models/tableOptions.model';
import { TableRouterLink } from '../../../../shared/components/table/models/tableRouterLink.model';


@Component({
    selector: 'esg-list-rating',
    templateUrl: './esgCompanyList.component.html',
    styleUrls: ['./esgCompanyList.component.scss', '../../../../../assets/table.scss'],
    providers: [UtilsService, ResearchEsgRatingService]
})
export class EsgCompaniesListComponent {
    public sub: Subscription;
    public assessmentsList: any[];
    public companies: EsgListModel[];
    public columns: ColumnOptions[];
    public subIndustryList: any[];
    public source: any[];
    public elementCount: number;
    public filter: EsgFilterModel = new EsgFilterModel();
    public paginationPosition = PaginationPositionDisplayEnum.UpAndDown;

    @ViewChild('table') table: TableComponent;

    constructor(
        private rootComponent: AppComponent,
        private esgService: ResearchEsgRatingService,
        private http: HttpClient) { }

    ngOnInit() {
        this.getSubIndustriesList();
        this.getBulkCompanies(new TableOptions("CompanyName"));
        this.getAssessmentsList();
    }

    public getCompanyUrlFriendlyName(company: EsgListModel): string {
        return UtilsService.getCompanyUrlFriendlyName(company);
    }

    public getAssessmentsList(): void {
        this.esgService.getEsgPerfomanceAssessmentsList().subscribe(data => {
            this.assessmentsList = data;
        });
    }

    public getSubIndustriesList(): void {
        this.esgService.getSubIndustriesList().subscribe(data => {
            this.subIndustryList = data;
            this.setColumns();
        });
    }

    private setColumns() {
        this.columns = [
            new ColumnOptions('CompanyName', 'companyName', 'Company Name', 'col-md-3', true, false, false, undefined, false, true, false),
            new ColumnOptions('Exchange', 'Exchange', 'Exchange', 'col-md-1', false, true, false, undefined, false, false, false, true,  undefined, false, false, true),
            new ColumnOptions('Ticker', 'Ticker', 'Ticker', 'col-md-1', false, true, false, undefined, false, false, false, true,  undefined, false, false, true),
            new ColumnOptions('Assessment', 'assessments', 'ESG Performance Assessment', 'col-md-3', false, true, false, undefined /*this.assessmentsList*/, false, false, false,true, undefined, false, false, true),
            new ColumnOptions('SubPeerGroup', 'subPeerGroup', 'SubIndustry', 'col-md-3', false, true, false, undefined/*this.subIndustryList*/, false, false, false,true,undefined, false, false, true),
            new ColumnOptions('LastUpdate', 'LastUpdate', 'Last Update', 'col-md-2', false, true, false, undefined, false, false, false)
        ];
    }

    private getBulkCompanies(tableOptions: TableOptions): void {
        var obsGet: EsgListModel[] = this.esgService.getCompanies(tableOptions.filter, tableOptions.pageOptions.skip, tableOptions.pageOptions.take, tableOptions.orderByCol, tableOptions.orderByAsc);
        var obsArray: any[] = [obsGet];
        if (tableOptions.pageOptions.listOnly == false) {
            let obsCount: Observable<CompanyCountModel> = this.esgService.getCompaniesCount(this.filter);
            obsArray.push(obsCount);
        }

        Observable.forkJoin(obsArray).subscribe((data) => {
            this.companies = data[0] as EsgListModel[];
            if (tableOptions.pageOptions.listOnly == false) {
                this.elementCount = (data[1] as CompanyCountModel).companyCount;
                this.table.ResetPaginationToFirstPage();
                this.rootComponent.finishLoading();
            }

            let valuesToDisplay = this.companies.map(company => {
                return {
                    company: new TableRouterLink(company.companyName, company.isEnabled, this.getCompanyUrlFriendlyName(company), 'esg'),
                    exchange: company.exchange,
                    ticker: company.ticker,
                    assessment: company.assessment,
                    subPeerGroup: company.subPeerGroup,
                    lastUpdate: new Date(company.lastUpdate)
                };
            })
            this.source = valuesToDisplay;
        });
    }

    public tableOptionsChanged(tableOptions: TableOptions): void {
        this.getBulkCompanies(tableOptions);
    }
}